package cn.lili.listener;

import cn.lili.common.security.AuthUser;
import cn.lili.event.*;
import cn.lili.modules.member.client.MemberSignClient;
import cn.lili.modules.member.entity.dos.MemberSign;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.payment.entity.dto.UserFmoneyUpdateDTO;
import cn.lili.modules.payment.entity.dto.UserPointUpdateDTO;
import cn.lili.modules.payment.entity.dto.UserWithdrawalMessage;
import cn.lili.routing.UserRoutingKey;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 用户消息
 *
 * @author paulG
 * @since 2020/12/9
 **/
@Component
@Slf4j
@RequiredArgsConstructor
public class UserMessageListener {

    /**
     * 用户签到
     */
    private final MemberSignClient memberSignClient;
    /**
     * 用户积分变化
     */
    private final List<UserPointChangeEvent> userPointChangeEvents;
    /**
     * 用户F分变化
     */
    private final List<UserFMoneyChangeEvent> userFMoneyChangeEvents;
    /**
     * 用户提现
     */
    private final List<UserWithdrawalEvent> userWithdrawalEvents;
    /**
     * 用户注册
     */
    private final List<UserRegisterEvent> userRegisterEvents;

    /**
     * 用户登录
     */
    private final List<UserLoginEvent> userLoginEvents;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${lili.amqp.user}" + "_" + UserRoutingKey.USER_REGISTER),
            exchange = @Exchange(value = "${lili.amqp.user}"),
            key = UserRoutingKey.USER_REGISTER))
    public void userRegister(User user) {
        for (UserRegisterEvent userRegisterEvent : userRegisterEvents) {
            try {
                userRegisterEvent.userRegister(user);
            } catch (Exception e) {
                log.error("用户{},在{}业务中，状态修改事件执行异常:{}",
                        user,
                        userRegisterEvent.getClass().getName(),
                        e);
            }
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${lili.amqp.user}" + "_" + UserRoutingKey.USER_LOGIN),
            exchange = @Exchange(value = "${lili.amqp.user}"),
            key = UserRoutingKey.USER_LOGIN))
    public void userLogin(AuthUser authUser) {

        for (UserLoginEvent userLoginEvent : userLoginEvents) {
            try {
                userLoginEvent.userLogin(authUser);
            } catch (Exception e) {
                log.error("用户{},在{}业务中，状态修改事件执行异常",
                        authUser,
                        userLoginEvent.getClass().getName(),
                        e);
            }
        }
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${lili.amqp.user}" + "_" + UserRoutingKey.USER_SING),
            exchange = @Exchange(value = "${lili.amqp.user}"),
            key = UserRoutingKey.USER_SING))
    public void userSign(MemberSign memberSign) {
        memberSignClient.memberSignSendPoint(memberSign.getMemberId(), memberSign.getSignDay());
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${lili.amqp.user}" + "_" + UserRoutingKey.USER_POINT_CHANGE),
            exchange = @Exchange(value = "${lili.amqp.user}"),
            key = UserRoutingKey.USER_POINT_CHANGE))
    public void userPointChange(UserPointUpdateDTO userPointUpdateDTO) {
        for (UserPointChangeEvent userPointChangeEvent : userPointChangeEvents) {
            try {
                userPointChangeEvent.userPointChange(userPointUpdateDTO);
            } catch (Exception e) {
                log.error("用户{},在{}业务中，状态修改事件执行异常",
                        userPointUpdateDTO,
                        userPointChangeEvent.getClass().getName(),
                        e);
            }
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${lili.amqp.user}" + "_" + UserRoutingKey.USER_FMONEY_CHANGE),
            exchange = @Exchange(value = "${lili.amqp.user}"),
            key = UserRoutingKey.USER_FMONEY_CHANGE))
    public void userFPointChange(UserFmoneyUpdateDTO userFmoneyUpdateDTO) {
        for (UserFMoneyChangeEvent userFMoneyChangeEvent : userFMoneyChangeEvents) {
            try {
                userFMoneyChangeEvent.userFPointChange(userFmoneyUpdateDTO);
            } catch (Exception e) {
                log.error("用户{},在{}业务中，状态修改事件执行异常",
                        userFmoneyUpdateDTO,
                        userFMoneyChangeEvent.getClass().getName(),
                        e);
            }
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${lili.amqp.user}" + "_" + UserRoutingKey.USER_WITHDRAWAL),
            exchange = @Exchange(value = "${lili.amqp.user}"),
            key = UserRoutingKey.USER_WITHDRAWAL))
    public void userWithdrawal(UserWithdrawalMessage userWithdrawalMessage) {

        for (UserWithdrawalEvent userWithdrawalEvent : userWithdrawalEvents) {
            try {
                userWithdrawalEvent.userWithdrawal(userWithdrawalMessage);
            } catch (Exception e) {
                log.error("用户{},在{}业务中，提现事件执行异常",
                        userWithdrawalMessage,
                        userWithdrawalEvent.getClass().getName(),
                        e);
            }
        }
    }

}
